﻿using AutoClickTool.Common;
using AutoClickTool.Enums;
using System.Timers;

namespace AutoClickTool.Models
{
    public class ActionModel
    {
        public ActionModel()
        {
            ActionStatus = ActionStatusEnum.Stop;
            ClickModels = new();
        }
        /// <summary>
        /// 控制键
        /// </summary>
        public string? Key { get; set; }
        /// <summary>
        /// 控制键名称
        /// </summary>
        public string? Name { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public List<ClickModel> ClickModels { get;private set; }
        /// <summary>
        /// 定时器
        /// </summary>
        public System.Timers.Timer? Timer { get; set; }
        /// <summary>
        /// 动作执行状态
        /// </summary>
        public ActionStatusEnum ActionStatus { get;private set; }
        /// <summary>
        /// 是否后台运行
        /// </summary>
        public bool IsBackTask { get; set; }
        /// <summary>
        /// APP名称
        /// </summary>
        public string? AppName { get; set; }
        /// <summary>
        /// 是否自动抬起
        /// </summary>
        public bool AutoUp { get; set; }
        /// <summary>
        /// 获取句柄
        /// </summary>
        public IntPtr? TagWindow { get; set; }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="model"></param>
        public void AddClickModel(ClickModel model)
        {
            ClickModels.Add(model);
        }
        /// <summary>
        /// 获取
        /// </summary>
        /// <returns></returns>
        public List<ClickModel> GetClickModels()
        {
            return ClickModels.OrderBy(w => w.Sort).ToList();
        }
        public ActionModel StartTimer(ElapsedEventHandler eventHandler, bool AutoReset = true)
        {
            if (Timer != null)
            {
                if (IsStop)
                {
                    Timer.AutoReset = AutoReset;
                    Timer.Elapsed += eventHandler;
                    Timer.Start();
                }
                else
                {
                    Timer.Start();
                }
            }
            return this;
        }
        public void Running()
        {
            ActionStatus = ActionStatusEnum.Running;
        }
        /// <summary>
        /// 是否运行中
        /// </summary>
        public bool IsRunning => ActionStatus == ActionStatusEnum.Running;
        public ActionModel StopTimer()
        {
            if (ActionStatus != ActionStatusEnum.Running)
            {
                throw new Exception("不在运行状态，无法暂停！");
            }
            Timer?.Stop();
            return this;
        }
        public void Suspend()
        {
            if (ActionStatus != ActionStatusEnum.Running)
            {
                throw new Exception("不在运行状态，无法暂停！");
            }
            ActionStatus = ActionStatusEnum.Suspend;
        }
        /// <summary>
        /// 是否暂停
        /// </summary>
        public bool IsSuspend => ActionStatus == ActionStatusEnum.Suspend;
        public void Stop()
        {
            ActionStatus = ActionStatusEnum.Stop;
        }
        /// <summary>
        /// 是否结束
        /// </summary>
        public bool IsStop => ActionStatus == ActionStatusEnum.Stop;
    }
}
